package com.cyf.service;

import com.cyf.db.bean.AppCar;
import com.cyf.db.bean.User;
import com.cyf.db.bean.ParkingLockInfo;
import com.cyf.db.bean.UsersWechat;
import com.cyf.db.mapper.AppUserCarMapper;
import com.cyf.db.mapper.UserMapper;
import com.cyf.db.mapper.ParkingLockInfoMapper;
import com.cyf.db.mapper.UsersWechatMapper;
import com.cyf.service.bean.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by cage on 2017/4/21.
 */
@Component
public class CheckParam {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AppUserCarMapper appUserCarMapper;
    @Autowired
    private ParkingLockInfoMapper lockInfoMapper;
    @Autowired
    private UsersWechatMapper usersWechatMapper;

    /**
     * 获取app用户注册的所有车辆
     *
     * @param token
     * @return
     * @throws CustomException
     */
    public List<AppCar> getAppCars(String token) throws CustomException {
        User user = getAppUser(token);
        return getAppCars(user);
    }

    /**
     * 获取微信公众号用户注册的所有车辆
     * @param openid
     * @return
     * @throws CustomException
     */
    public List<AppCar> getAppCarsByOpenid(String openid) throws CustomException {
        User user = getUserByOpenid(openid);
        return getAppCars(user);
    }


    public List<AppCar> getAppCars(User user) throws CustomException {
        List<AppCar> appCars = appUserCarMapper.getCarByUserId(user.getId());
        if (appCars == null || appCars.size() == 0) {
            throw new CustomException("用户未注册车辆！");
        }
        return appCars;
    }

    /**
     * 获取app用户注册的车辆
     *
     * @param token
     * @param plate
     * @return
     * @throws CustomException
     */
    public AppCar getAppCar(String token, String plate) throws CustomException {
        List<AppCar> appCars = getAppCars(token);
        for (AppCar appCar : appCars) {
            if (appCar.getPlate().equals(plate)) {
                return appCar;
            }
        }
        throw new CustomException("用户未注册该车辆");
    }

    public AppCar getAppCar(User user, String plate) throws CustomException {
        List<AppCar> appCars = getAppCars(user);
        for (AppCar appCar : appCars) {
            if (appCar.getPlate().equals(plate)) {
                return appCar;
            }
        }
        throw new CustomException("用户未注册该车辆");
    }

    public User getAppUser(String token) throws CustomException {
        if (token != null && !"".equals(token)) {
            User user = userMapper.getByToken(token);
            if (user != null) {
                return user;
            }
        }
        throw new CustomException("登录超时！");
    }

    public User getUserByOpenid(String openid) throws CustomException {
        UsersWechat usersWechat = usersWechatMapper.getByOpenid(openid);
        if (usersWechat != null) {
            User user = userMapper.getById(usersWechat.getUserId());
            if (user != null) {
                return user;
            }
        }
        throw new CustomException("用户不存在！");
    }

    /**
     * 检查app用户是否存在名字为xx或mac地址为xx的地锁（除了地锁id为xx的地锁）
     *
     * @param appUserId  APP用户id
     * @param lockName   地锁名称
     * @param macAddress mac地址
     * @param lockId     地锁id
     */
    public void checkLockExist(int appUserId, String lockName, String macAddress, int lockId) throws CustomException {
        List<ParkingLockInfo> lockInfos = lockInfoMapper.getByAppUserId(appUserId);
        if (lockInfos != null && lockInfos.size() > 0) {
            for (ParkingLockInfo lockInfo : lockInfos) {
                if (lockInfo.getId() == lockId) {
                    continue;
                }
                if (lockName.equals(lockInfo.getName())) {
                    throw new CustomException("该地锁名称已存在！");
                }
                if (macAddress.equals(lockInfo.getMacAddress())) {
                    throw new CustomException("该地锁已经添加！");
                }
            }
        }
        ParkingLockInfo lockInfo = lockInfoMapper.getByMacAddress(macAddress);
        if (lockInfo != null && lockInfo.getId() != lockId) {
            throw new CustomException("该地锁已经添加！");
        }
    }

    /**
     * 根据appUserId和地锁id查找地锁
     *
     * @param appUserId     APP用户id
     * @param parkingLockId 地锁id
     * @return ParkingLockInfo
     */
    public ParkingLockInfo getLock(int appUserId, int parkingLockId) throws CustomException {
        ParkingLockInfo lockInfo = lockInfoMapper.getByAppUserIdAndId(appUserId, parkingLockId);
        if (lockInfo == null) {
            throw new CustomException("用户没有该地锁！");
        }
        return lockInfo;
    }


}
